﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Common;
using SYH178Utils;

namespace SYH178DAL
{
    public class Company_Info
    {
        public Company_Info()
        { }
        #region  BasicMethod 后台代码 
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SYH178Model.Company_Info model, string Introduction)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyType", SqlDbType.Int,4),
					new SqlParameter("@BusinessModel", SqlDbType.Int,4),
					new SqlParameter("@LinkMan", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@TelPhone", SqlDbType.VarChar,50),
					new SqlParameter("@Fax", SqlDbType.VarChar,50),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@BDateTime", SqlDbType.DateTime),
					new SqlParameter("@RegisterAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Postcode", SqlDbType.VarChar,50),
					new SqlParameter("@URL", SqlDbType.VarChar,50),
					new SqlParameter("@IndustryId", SqlDbType.Int,4),
					new SqlParameter("@CompanyAddress", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@Img", SqlDbType.VarChar,50),
					new SqlParameter("@UserId", SqlDbType.Int,4),
                    new SqlParameter("@Introduction", SqlDbType.NVarChar,-1)                   };
            parameters[0].Value = model.Name;
            parameters[1].Value = model.CompanyType;
            parameters[2].Value = model.BusinessModel;
            parameters[3].Value = model.LinkMan;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.TelPhone;
            parameters[6].Value = model.Fax;
            parameters[7].Value = model.Email;
            parameters[8].Value = model.BDateTime;
            parameters[9].Value = model.RegisterAddress;
            parameters[10].Value = model.Address;
            parameters[11].Value = model.Postcode;
            parameters[12].Value = model.URL;
            parameters[13].Value = model.IndustryId;
            parameters[14].Value = model.CompanyAddress;
            parameters[15].Value = model.Type;
            parameters[16].Value = model.Status;
            parameters[17].Value = model.CreateTime;
            parameters[18].Value = model.EndTime;
            parameters[19].Value = model.Img;
            parameters[20].Value = model.UserId;
            parameters[21].Value = Introduction;
            object obj = SqlHelper.ExecuteNonQuery(Config.SYHConnStr, CommandType.StoredProcedure, "Company_Info_Add", parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYH178Model.Company_Info model, string Introduction)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@CompanyType", SqlDbType.Int,4),
					new SqlParameter("@BusinessModel", SqlDbType.Int,4),
					new SqlParameter("@LinkMan", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@TelPhone", SqlDbType.VarChar,50),
					new SqlParameter("@Fax", SqlDbType.VarChar,50),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@BDateTime", SqlDbType.DateTime),
					new SqlParameter("@RegisterAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Postcode", SqlDbType.VarChar,50),
					new SqlParameter("@URL", SqlDbType.VarChar,50),
					new SqlParameter("@IndustryId", SqlDbType.Int,4),
					new SqlParameter("@CompanyAddress", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@Img", SqlDbType.VarChar,50),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4),
                    new SqlParameter("@Introduction", SqlDbType.NVarChar,-1)                   };
            parameters[0].Value = model.Name;
            parameters[1].Value = model.CompanyType;
            parameters[2].Value = model.BusinessModel;
            parameters[3].Value = model.LinkMan;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.TelPhone;
            parameters[6].Value = model.Fax;
            parameters[7].Value = model.Email;
            parameters[8].Value = model.BDateTime;
            parameters[9].Value = model.RegisterAddress;
            parameters[10].Value = model.Address;
            parameters[11].Value = model.Postcode;
            parameters[12].Value = model.URL;
            parameters[13].Value = model.IndustryId;
            parameters[14].Value = model.CompanyAddress;
            parameters[15].Value = model.Type;
            parameters[16].Value = model.Status;
            parameters[17].Value = model.CreateTime;
            parameters[18].Value = model.EndTime;
            parameters[19].Value = model.Img;
            parameters[20].Value = model.UserId;
            parameters[21].Value = model.Id;
            parameters[22].Value = Introduction;
            int rows = SqlHelper.ExecuteNonQuery(Config.SYHConnStr, CommandType.StoredProcedure, "Company_Info_Update", parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = SqlHelper.ExecuteNonQuery(Config.SYHConnStr, CommandType.StoredProcedure, "Company_Info_Delete", parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH178Model.Company_Info GetModel(int UserId)
        {

            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4)
			};
            parameters[0].Value = UserId;

            SYH178Model.Company_Info model = new SYH178Model.Company_Info();
            DataSet ds = SqlHelper.ExecuteDataset(Config.SYHConnStr, "Company_Info_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH178Model.Company_Info DataRowToModel(DataRow row)
        {
            SYH178Model.Company_Info model = new SYH178Model.Company_Info();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["CompanyType"] != null && row["CompanyType"].ToString() != "")
                {
                    model.CompanyType = int.Parse(row["CompanyType"].ToString());
                }
                if (row["BusinessModel"] != null && row["BusinessModel"].ToString() != "")
                {
                    model.BusinessModel = int.Parse(row["BusinessModel"].ToString());
                }
                if (row["LinkMan"] != null)
                {
                    model.LinkMan = row["LinkMan"].ToString();
                }
                if (row["Mobile"] != null)
                {
                    model.Mobile = row["Mobile"].ToString();
                }
                if (row["TelPhone"] != null)
                {
                    model.TelPhone = row["TelPhone"].ToString();
                }
                if (row["Fax"] != null)
                {
                    model.Fax = row["Fax"].ToString();
                }
                if (row["Email"] != null)
                {
                    model.Email = row["Email"].ToString();
                }
                if (row["BDateTime"] != null && row["BDateTime"].ToString() != "")
                {
                    model.BDateTime = DateTime.Parse(row["BDateTime"].ToString());
                }
                if (row["RegisterAddress"] != null)
                {
                    model.RegisterAddress = row["RegisterAddress"].ToString();
                }
                if (row["Address"] != null)
                {
                    model.Address = row["Address"].ToString();
                }
                if (row["Postcode"] != null)
                {
                    model.Postcode = row["Postcode"].ToString();
                }
                if (row["URL"] != null)
                {
                    model.URL = row["URL"].ToString();
                }
                if (row["IndustryId"] != null && row["IndustryId"].ToString() != "")
                {
                    model.IndustryId = int.Parse(row["IndustryId"].ToString());
                }
                if (row["CompanyAddress"] != null)
                {
                    model.CompanyAddress = row["CompanyAddress"].ToString();
                }
                if (row["Type"] != null && row["Type"].ToString() != "")
                {
                    model.Type = int.Parse(row["Type"].ToString());
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["EndTime"] != null && row["EndTime"].ToString() != "")
                {
                    model.EndTime = DateTime.Parse(row["EndTime"].ToString());
                }
                if (row["Img"] != null)
                {
                    model.Img = row["Img"].ToString();
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
            }
            return model;
        }


        //根据编号得到公司名(为公司URL)
        public DataTable GetComapnyNameById(int id)
        {
            string sql = "  select Id,Name as CompanyName from Company_Info where Id=" + id;

            return SqlHelper.ExecuteDataset(Config.SYHConnStr, CommandType.Text, sql).Tables[0];
        }

        #endregion  BasicMethod

        #region  ExtensionMethod 前台代码

        #endregion  ExtensionMethod
    }
}
